package cn.dgut.jy.soro.controller;

import cn.dgut.jy.soro.entity.CartItem;
import cn.dgut.jy.soro.entity.Product;
import cn.dgut.jy.soro.service.CartItemService;
import cn.dgut.jy.soro.service.CartService;
import cn.dgut.jy.soro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class CartController {
    private CartService cartService;
    private CartItemService cartItemService;
    private UserService userService;

    @Autowired
    public CartController(CartService cartService, CartItemService cartItemService, UserService userService) {
        this.cartService = cartService;
        this.cartItemService = cartItemService;
        this.userService = userService;
    }

    @RequestMapping(value = "/add_item", method = RequestMethod.GET)
    public String addItem(Model model, @RequestParam Integer userId, @RequestParam Integer productId, @RequestParam Integer amount) {
        if(userId == 0) {
            model.addAttribute("message", "请先登录");
            return "/login";
        }

        cartItemService.addItem(userId, productId, amount);
        return "/shop";
    }


    @Transactional
    @RequestMapping(value = "/remove_item", method = RequestMethod.GET)
    public void removeItem(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                           @RequestParam Integer productId) throws IOException {
        Integer userId = (Integer) session.getAttribute("userId");
        if(userId == null) {
            response.sendRedirect(request.getContextPath() + "/login");
        }
        cartItemService.removeItem(userId, productId);
        response.sendRedirect(request.getContextPath() + "/cart");
    }

    @RequestMapping("/cart")
    public String cart(Model model, HttpSession httpSession) {
        //根据userId找到对应的购物车中的所有购物车项

        Integer userId = (Integer) httpSession.getAttribute("userId");

        if(userId == null) {
            model.addAttribute("message", "请先登录");
            return "/login";
        }

        List<CartItem> cartItems = cartService.getCartItemsByUserId(userId);
        List<Product> products = cartItemService.getProductsByCartItems(cartItems);

        if(cartItems.isEmpty()) {
            return "/cartEmpty";
        }

        model.addAttribute("cartItems", cartItems);
        model.addAttribute("products", products);
        model.addAttribute("totalPrice" , cartService.getTotalPriceByUserId(userId));
        System.out.println(cartService.getTotalPriceByUserId(userId));
        System.out.println("测试！");

        //model.addAttribute();
        return "/cart";
    }

    @RequestMapping("/buy")
    public String buy(HttpSession session, Model model, @RequestParam String str) {

        Integer userId = (Integer) session.getAttribute("userId");

        String [] strs = str.split(",");
        for (String s: strs
             ) {
            System.out.println(s);
        }

        int length = strs.length;
        Integer [] productIds = new Integer[length];


        for (int i = 0; i < length; i++) {

            productIds[i] = Integer.parseInt(strs[i]);
        }

        List<CartItem> cartItems = cartService.getCartItemsByUserIdAndProductIds(userId, productIds);
        List<Product> products = cartItemService.getProductsByCartItems(cartItems);
        model.addAttribute("cartItems", cartItems);
        model.addAttribute("products", products);


        return "/buy";
    }


}
